//index.js
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var config = require('../../config')
var util = require('../../utils/util.js')

Page({
    data: {
        logged: false,
        takeSession: false,
        requestResult: '',
        isDis:true,
        timer:null
    },
    onShow:function(){
      this.firstPage(1);
    },
    firstPage(i){
      if(!this.data.isDis) return;
      const that=this;
      if(this.timer) this.setData({timer:clearTimeout(this.timer)});
      this.setData({
        timer:setTimeout(() => {
        if(!getApp().data.logged){
          // console.log(i);
          if(i>10){
            wx.redirectTo({
              url: '../login/login',
            })
          }else{
            this.setData({
              logged: getApp().data.logged
            })
            that.firstPage(++i);
          }
        }else{
          
          setTimeout(()=>{
            wx.redirectTo({
              url: '../home/home',
            })
          },100)
         
        }
      },500)});
    },
    onUnload(){
      clearTimeout(this.timer);
      this.setData({ isDis:false})
    }
    ,
    testlogin:function(){
      const app=getApp();
      console.log(app.data);
      app.data.logged=true;
      console.log(app.data);
    },
    bindGetUserInfo: function (e) {
      if (this.data.logged) return;

      util.showBusy('正在登录');

      var that = this;
      var userInfo = e.detail.userInfo;

      // 查看是否授权
      wx.getSetting({
        success: function (res) {
          if (res.authSetting['scope.userInfo']) {

            // 检查登录是否过期
            wx.checkSession({
              success: function () {
                // 登录态未过期
                util.showSuccess('登录成功');
                that.setData({
                  userInfo: userInfo,
                  logged: true
                })
              },

              fail: function () {
                qcloud.clearSession();
                // 登录态已过期，需重新登录
                var options = {
                  encryptedData: e.detail.encryptedData,
                  iv: e.detail.iv,
                  userInfo: userInfo
                }
                that.doLogin(options);
              },
            });
          } else {
            util.showModel('用户未授权', e.detail.errMsg);
          }
        }
      });
    },

    doLogin: function(options) {
      var that = this;

      wx.login({
        success: function (loginResult) {
          var loginParams = {
            code: loginResult.code,
            encryptedData: options.encryptedData,
            iv: options.iv,
          }
          qcloud.requestLogin({
            loginParams, success() {
              util.showSuccess('登录成功');

              that.setData({
                userInfo: options.userInfo,
                logged: true
              })
            },
            fail(error) {
              util.showModel('登录失败', error)
              console.log('登录失败', error)
            }
          });
        },
        fail: function (loginError) {
          util.showModel('登录失败', loginError)
          console.log('登录失败', loginError)
        },
      });
    },

    // 切换是否带有登录态
    switchRequestMode: function (e) {
        this.setData({
            takeSession: e.detail.value
        })
        this.doRequest()
    },

    doRequest: function () {
        util.showBusy('请求中...')
        var that = this
        var options = {
            url: config.service.requestUrl,
            login: true,
            success (result) {
                util.showSuccess('请求成功完成')
                console.log('request success', result)
                that.setData({
                    requestResult: JSON.stringify(result.data)
                })
            },
            fail (error) {
                util.showModel('请求失败', error);
                console.log('request fail', error);
            }
        }
        if (this.data.takeSession) {  // 使用 qcloud.request 带登录态登录
            qcloud.request(options)
        } else {    // 使用 wx.request 则不带登录态
            wx.request(options)
        }
    },
    
  
})
